package softtech.apps.autoanswerphonecall;



import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.startapp.android.publish.StartAppAd;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;



//with this
//import com.google.android.gms.ads.*;


public class MainActivity extends Activity {

	 /** The view to show the ad. */
	private AdView adView;

	private StartAppAd startAppAd = new StartAppAd(this);

	static boolean started = false;
	// Ads
	int enable;
	int enable_id;
	ImageButton btnEnable;

	int enableVoice;
	int enableVoice_id;
	ImageButton btnEnableModeVoice;

	int enableSMS;
	int enableSMS_id;
	ImageButton btnEnableModeSMS;

	int mode = 0;

	DatabaseHandler db;

	Context context = this;
	
	AppCacheData cache = new AppCacheData();

	int enableVoiceMessageRecord;
	int enableVoiceMessageRecord_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.option);

		ActionBar bar = getActionBar();
		bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2ac2d4")));

		db = new DatabaseHandler(this);
		// Find setting button
		btnEnable = (ImageButton) findViewById(R.id.imgEnableModeButton);
		btnEnableModeVoice = (ImageButton) findViewById(R.id.imgEnableVoiceAnswer);
		btnEnableModeSMS = (ImageButton) findViewById(R.id.imgEnableAutoSMSButton);
		
		// Get all config from database
		Config cfgEnable = db.getConfig(1);
		if (!cfgEnable.equals(null) && cfgEnable != null) {
			enable = cfgEnable.get_value();
			enable_id = cfgEnable.get_id();
		}

		Config cfgVoice = db.getConfig(2);
		if (!cfgVoice.equals(null) && cfgVoice != null) {
			enableVoice = cfgVoice.get_value();
			enableVoice_id = cfgVoice.get_id();
		}

		Config cfgSMS = db.getConfig(3);

		if (!cfgSMS.equals(null) && cfgSMS != null) {
			enableSMS = cfgSMS.get_value();
			enableSMS_id = cfgSMS.get_id();
		}

		Config cfgRecordVoiceMessage = db.getConfig(5);
		if (!cfgRecordVoiceMessage.equals(null)
				&& cfgRecordVoiceMessage != null) {
			enableVoiceMessageRecord = cfgRecordVoiceMessage.get_value();
			enableVoiceMessageRecord_id = cfgRecordVoiceMessage.get_id();

		}
		
		// Enable/Disable incoming call receiver
		final ComponentName component = new ComponentName(context,
				PhoneCallReceiver.class);
		// Check status of receiver, if enable/disable -> update to database
		int status = context.getPackageManager().getComponentEnabledSetting(
				component);
		if (status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
			Config config = new Config();
			enable = 1;
			config.set_id(enable_id);
			config.set_value(enable);
			db.updateConfig(config);
			cache.SaveCache(context, "broadcast", "1");
			started = true;
			if(!started){
				Toast.makeText(getApplicationContext(),
						"Enabled Auto Answer Phone Call", Toast.LENGTH_SHORT)
						.show();
			}

		} else if (status == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
			Config config = new Config();
			enable = 0;
			config.set_id(enable_id);
			config.set_value(enable);
			db.updateConfig(config);
			cache.SaveCache(context, "broadcast", "0");
			
			if(!started){
				Toast.makeText(getApplicationContext(),
						"Disable Auto Answer Phone Call", Toast.LENGTH_SHORT)
						.show();
			}
		}

		Log.d("ENABLE", "enable FROM Database = " + enable);

		if (enable == 1) {
			btnEnable.setImageResource(R.drawable.on);
			cache.SaveCache(context, "broadcast", "1");

		} else {
			btnEnable.setImageResource(R.drawable.off);
			cache.SaveCache(context, "broadcast", "0");
		}

		if (enableVoice == 1) {
			btnEnableModeVoice.setImageResource(R.drawable.on);
		} else {
			btnEnableModeVoice.setImageResource(R.drawable.off);
		}

		if (enableSMS == 1) {
			btnEnableModeSMS.setImageResource(R.drawable.on);
		} else {
			btnEnableModeSMS.setImageResource(R.drawable.off);
		}

		// Click to enable/disable
		btnEnable.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Toast.makeText(getApplicationContext(), "FUck",
				// Toast.LENGTH_SHORT).show();

				Config config = new Config();
				if (enable == 1) {
					enable = 0;
					btnEnable.setImageResource(R.drawable.off);
					// Disable broadcast receive here
					// Disable

					cache.SaveCache(context, "broadcast", "0");
					 context.getPackageManager().setComponentEnabledSetting(component,
					 PackageManager.COMPONENT_ENABLED_STATE_DISABLED ,
					 PackageManager.DONT_KILL_APP);
					Toast.makeText(getBaseContext(),
							"Disable auto answer phone call",
							Toast.LENGTH_SHORT).show();

				} else {
					enable = 1;
					btnEnable.setImageResource(R.drawable.on);
					// Enable broadcast receive here
					cache.SaveCache(context, "broadcast", "1");
					// Enable
					 context.getPackageManager().setComponentEnabledSetting(component,
					 PackageManager.COMPONENT_ENABLED_STATE_ENABLED ,
					 PackageManager.DONT_KILL_APP);
					Toast.makeText(getBaseContext(),
							"Enable auto answer phone call", Toast.LENGTH_SHORT)
							.show();
				}

				config.set_id(enable_id);
				config.set_value(enable);

				Log.d("ENABLE", "enable = " + enable);
				int status = db.updateConfig(config);

				Log.d("UPDATE", "update status = " + status);
			}
		});

		btnEnableModeVoice.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Toast.makeText(getApplicationContext(), "FUck 2",
				// Toast.LENGTH_SHORT).show();
				Config config1 = new Config();

				Config config2 = new Config();
				config2.set_id(enableSMS_id);

				if (enableVoice == 1) {
					enableVoice = 0;
					btnEnableModeVoice.setImageResource(R.drawable.off);
					btnEnableModeSMS.setImageResource(R.drawable.on);
					config2.set_value(1);

				} else {
					enableVoice = 1;
					btnEnableModeVoice.setImageResource(R.drawable.on);
					config2.set_value(0);
					btnEnableModeSMS.setImageResource(R.drawable.off);
				}

				config1.set_id(enableVoice_id);
				config1.set_value(enableVoice);

				db.updateConfig(config1);
				db.updateConfig(config2);

				if (enableVoice == 1) {
					Intent toVoiceManagerActivity = new Intent(
							getBaseContext(), VoiceManagerActivity.class);
					toVoiceManagerActivity
							.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					getBaseContext().startActivity(toVoiceManagerActivity);
					mode = 1;
				}else{
					mode = 2;
				}
			}
		});

		btnEnableModeSMS.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Toast.makeText(getApplicationContext(), "FUck 3",
				// Toast.LENGTH_SHORT).show();
				Config config2 = new Config();

				Config config1 = new Config();
				config1.set_id(enableVoice_id);

				if (enableSMS == 1) {
					enableSMS = 0;
					btnEnableModeSMS.setImageResource(R.drawable.off);
					btnEnableModeVoice.setImageResource(R.drawable.on);
					config1.set_value(1);
				} else {
					enableSMS = 1;
					btnEnableModeSMS.setImageResource(R.drawable.on);
					config1.set_value(0);
					btnEnableModeVoice.setImageResource(R.drawable.off);

				}

				config2.set_id(enableSMS_id);
				config2.set_value(enableSMS);

				db.updateConfig(config1);
				db.updateConfig(config2);

				Log.d("ENDABLE", "Enable SMS = " + enableSMS);
				
				if (enableSMS == 1) {
					Intent toManagerActivity = new Intent(getBaseContext(),
							MessageManagerActivity.class);
					toManagerActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					getBaseContext().startActivity(toManagerActivity);

					mode = 2;
				}else{
					mode = 1;
				}

			}
		});

		TextView tvEnableVoiceAnswer = (TextView) findViewById(R.id.tvEnableVoiceAnswer);
		TextView tvEnableAutoSMS = (TextView) findViewById(R.id.tvEnableAutoSMS);

		// Click to edit details auto answer by voice
		tvEnableVoiceAnswer.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				Intent toVoiceManagerActivity = new Intent(getBaseContext(),
						VoiceManagerActivity.class);
				toVoiceManagerActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				getBaseContext().startActivity(toVoiceManagerActivity);
				return false;
			}
		});

		// Click to edit details auto reply by SMS
		tvEnableAutoSMS.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent toManagerActivity = new Intent(getBaseContext(),
						MessageManagerActivity.class);
				toManagerActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				getBaseContext().startActivity(toManagerActivity);
			}
		});

		TextView tvListenVoiceMessage = (TextView) findViewById(R.id.tvListenVoiceMessage);
		tvListenVoiceMessage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent toManagerActivity = new Intent(getBaseContext(),
						ListVoiceMessageActivity.class);
				toManagerActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				getBaseContext().startActivity(toManagerActivity);
			}
		});
		
		
		startService();
		
		// Google Admod
		
		// Look up the AdView as a resource and load a request.

		  adView = (AdView) this.findViewById(R.id.adView);

		  adView.loadAd(new AdRequest());
		  
		  // start app
		  StartAppAd.init(this, "107125836", "204382801");
		  
		  new Handler().postDelayed(new Runnable()
		     {
		         @Override
		         public void run()
		         {
		             startAppAd.showAd();
		             startAppAd.loadAd();
		         }
		     }, 2000);


	}

	public void disable() {
		ComponentName mReceiver = new ComponentName(context,
				PhoneCallReceiver.class);
		PackageManager mManager = context.getPackageManager();
		mManager.setComponentEnabledSetting(mReceiver,
				PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
				PackageManager.DONT_KILL_APP);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.config_manager, menu);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		// Handle presses on the action bar items
		switch (item.getItemId()) {
		case R.id.action_settings:
			// openSettings();
			Toast.makeText(getApplicationContext(), "Action bar click",
					Toast.LENGTH_SHORT).show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	void startService()
	 {
	  Log.e("MAIN ACTIVITY", "da start service");
	  context.startService(new Intent(context, MyService.class));
	 }

}
